package com.designpattern.strategy;

/**
 * 选择排序算法。
 */
public class Sorter {
    //初步
    public static void sort(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            int minPos = i;
            for (int j = i + 1; j < arr.length; j++) {
                minPos = arr[j] < arr[minPos] ? j : minPos;
            }
            swap(arr, i, minPos);
        }
    }

    static void swap(int[] arr, int i, int j) {
        int tem = arr[i];
        arr[i] = arr[j];
        arr[j] = tem;
    }
    //升级
    public static void sort(Cat[] arr) {
        for (int i = 0; i < arr.length; i++) {
            int minPos = i;
            for (int j = i + 1; j < arr.length; j++) {
                minPos = arr[j].compareTo(arr[minPos]) == -1 ? j : minPos;
            }
            swap(arr, i, minPos);
        }
    }

    static void swap(Cat[] arr, int i, int j) {
        Cat tem = arr[i];
        arr[i] = arr[j];
        arr[j] = tem;
    }

    //再升级
    public static void sort(MyComparable[] arr) {
        for (int i = 0; i < arr.length; i++) {
            int minPos = i;
            for (int j = i + 1; j < arr.length; j++) {
                minPos = arr[j].compareTo(arr[minPos]) == -1 ? j : minPos;
            }
            swap(arr, i, minPos);
        }
    }

    static void swap(MyComparable[] arr, int i, int j) {
        MyComparable tem = arr[i];
        arr[i] = arr[j];
        arr[j] = tem;
    }

}
